package com.atguigu.gulimall.product.web;

import com.atguigu.gulimall.product.entity.CategoryEntity;
import com.atguigu.gulimall.product.service.CategoryService;
import com.atguigu.gulimall.product.vo.Catelog2Vo;
import org.redisson.api.RLock;
import org.redisson.api.RReadWriteLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * @ClassName IndexController
 * @Description TODO
 * @Author Administrator
 * @Date 2022/6/21 18:49
 * @Version 1.0
 **/
@Controller
public class IndexController {

    @Autowired
    CategoryService categoryService;

    @Autowired
    RedissonClient redissonClient;

    @Autowired
    RedisTemplate redisTemplate;

    @GetMapping(value = {"/","/index.html"})
    public String indexPage(Model model){
        //TODO 1 查出所有的1级分类
        List<CategoryEntity> categoryEntityList = categoryService.getLevel1Categorys();
        model.addAttribute("categorys",categoryEntityList);
        return "index";
    }

    //index/catalog.json
    @ResponseBody
    @GetMapping("/index/catalog.json")
    public Map<String, List<Catelog2Vo>> getCatalogJson(){
        Map<String, List<Catelog2Vo>> map =  categoryService.getCatalogJson();
        return map;
    }


    @ResponseBody
    @GetMapping("/hello")
    public String hello(){
        //第一步，获取一把锁，只要锁名一样就能锁住，就是同一把锁
        RLock mylock = redissonClient.getLock("mylock");

        //加锁
//        mylock.lock(); //阻塞式等待 默认加锁30s
        //1））锁的自动续期，如果业务超长，会为锁续上系新的30s，不用担心业务时间超长，锁自动过期被删
        //2)) 加锁的业务只要完成，就不会给当前锁续期，30s后默认删除
        //最佳实战
        mylock.lock(30, TimeUnit.SECONDS);//自动解锁的时间一定要大于业务的执行时间  锁过期后不会自动续期
        try {
            System.out.println("加锁成功，执行业务..."+Thread.currentThread().getName());
            Thread.sleep(30000);
        }catch (Exception e){

        } finally{
            //解锁  假设解锁代码没有执行，redisson会不会出现死锁
            System.out.println("释放锁"+Thread.currentThread().getName());
            mylock.unlock();
        }

        return "hello";
    }


    //保证一定能读到最新数据，修改期间，写锁是一个排他锁（互斥锁），读锁是一个共享锁
    //写锁没释放读锁就必须等待
    @ResponseBody
    @GetMapping("/write")
    public String writeValue(){
        String s = "";
        RReadWriteLock lock = redissonClient.getReadWriteLock("rw-lock");
        RLock rLock = lock.writeLock();
        try {

            rLock.lock();
            s = UUID.randomUUID().toString();
            redisTemplate.opsForValue().set("writeValue",s);
            Thread.sleep(30000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }finally {
            rLock.unlock();
        }
        return s;
    }

    @ResponseBody
    @GetMapping("/read")
    public String readValue(){
        String s = "";
        RReadWriteLock lock = redissonClient.getReadWriteLock("rw-lock");
        //加读锁
        RLock rLock = lock.readLock();
        rLock.lock();
        try {
            s = (String) redisTemplate.opsForValue().get("writeValue");
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            rLock.unlock();
        }
        return s;
    }

}
